package jgl.vajra.management.part.home.pop;

import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;

import java.util.HashMap;
import java.util.Map;

import jgl.vajra.management.R;
import jgl.vajra.management.databinding.PopModifyPriceBinding;
import jgl.vajra.management.manager.AccountManager;
import jgl.vajra.management.widget.manager.RxJavaHttpManager;
import jgl.vajra.management.widget.manager.retrofit.RetrofitJsonManager;
import jgl.vajra.management.widget.retrofithelper.rxsubscriber.ProgressObserver;
import jgl.vajra.management.widget.utils.ToastUtils;
import jgl.vajra.management.widget.utils.UIUtils;


public class ModifyPricePop {

    private FragmentActivity activity;
    private View mPopRootView;
    private PopupWindow menuPop;
    private ModifyPriceListener modifyPriceListener;
    private PopModifyPriceBinding mBinding;
    private String order;
    private String logo;
    private String amount;
    private String newAmount;

    public ModifyPricePop(FragmentActivity activity, String  newAmount,  String order,  String logo) {
        this.activity = activity;
        this.newAmount = newAmount;
        this.order = order;
        this.logo = logo;
    }


    private void initPopupWindow() {
        mPopRootView = View.inflate(UIUtils.getContext(), R.layout.pop_modify_price, null);
        mBinding = DataBindingUtil.bind(mPopRootView);
        mBinding.setData(newAmount);

        menuPop = new PopupWindow(mPopRootView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        menuPop.setOutsideTouchable(false);
        menuPop.setBackgroundDrawable(new BitmapDrawable());
        menuPop.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
        //输入框被键盘遮盖
        menuPop.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        menuPop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        menuPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //popupwindow消失的时候恢复成原来的透明度
                backgroundAlpha(1f);
            }
        });
        mBinding.setView(this);
    }

    public void showPopupWindow(View v) {
        if (menuPop == null)
            initPopupWindow();
        int[] location = new int[2];
        v.getLocationOnScreen(location);  //获取控件的位置坐标
        //获取自身的长宽高
        mPopRootView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        backgroundAlpha(0.5f);

        menuPop.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    public boolean isShowing() {
        return menuPop != null && menuPop.isShowing();
    }

    public void dismiss() {
        if (menuPop != null && menuPop.isShowing())
            menuPop.dismiss();
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        if (activity == null) return;
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        activity.getWindow().setAttributes(lp);

    }


    public void setModifyPriceListener(ModifyPriceListener listener) {
        modifyPriceListener = listener;

    }

    public interface ModifyPriceListener {
        void onDetermine(String amount);
    }

    //确定
    public void onDetermine() {
        amount = mBinding.etNum.getText().toString().trim();
        if (TextUtils.isEmpty(amount)) {
            ToastUtils.showShort("我的出价不能为空");
            return;
        }

        double floorPrice = Integer.parseInt(newAmount) * 1.03;
        double ceilingPrice = Integer.parseInt(newAmount) * 1.5;

        if (floorPrice > Double.parseDouble(amount)) {
            ToastUtils.showShort("我的出价不能低于客户出价" + floorPrice);
            return;
        }
        if (ceilingPrice < Double.parseDouble(amount)) {
            ToastUtils.showShort("我的出价不能高于客户出价" + ceilingPrice);
            return;
        }



        Map<String, Object> map = new HashMap<>();
        map.put("orderId", order);
        map.put("amount", amount);
        map.put("logo", logo);

        //车主出价
        RetrofitJsonManager.getInstance().getApiService().ownerBid(map, AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization)
                .compose(RxJavaHttpManager.applyTransformer())
                .subscribe(new ProgressObserver<String>(true, null) {
                    @Override
                    public void _onNext(String data) {
                        ToastUtils.showShort(data);
                        dismiss();
                        if (modifyPriceListener != null) {
                            modifyPriceListener.onDetermine(amount);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage, int errorCode) {
                        super._onError(errorMessage, errorCode);
                        ToastUtils.showShort(errorMessage);
                    }
                });

    }
}


